package com.wclass.controller.app;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.wclass.common.ReturnObject;
import com.wclass.entity.CourseInfo;
import com.wclass.entity.CourseWare;
import com.wclass.interceptor.BaseContextHandler;
import com.wclass.interceptor.IgnoreAuth;
import com.wclass.oss.QiniuCloudStorageService;
import com.wclass.service.IBaseAppUserService;
import com.wclass.service.ICourseInfoService;
import com.wclass.service.ICourseWareService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课件 前端控制器
 * </p>
 *
 * @author 张浩高
 * @since 2018-12-20
 */
@Api(description = "课件")
@RestController
@RequestMapping("/courseWare")
public class CourseWareController {

    @Autowired
    private ICourseWareService iCourseWareService;
    @Autowired
    private IBaseAppUserService iBaseAppUserService;
    @Autowired
    private ICourseInfoService iCourseInfoService;

    @ApiOperation("新增课件材料")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ReturnObject add(@ApiParam("课程id") @RequestParam Long courseInfoId,
                            @ApiParam("课件名称") @RequestParam String name,
                            @ApiParam("附件地址") @RequestParam String attachmentUrl,
                            @ApiParam("附件大小") @RequestParam Long attachmentSize,
                            @ApiParam("附件类型") @RequestParam String attachmentType) {
        CourseWare courseWare = new CourseWare(0, name, BaseContextHandler.getUserID(), null, courseInfoId, attachmentUrl, attachmentSize, attachmentType);
        iCourseWareService.insert(courseWare);
        return new ReturnObject(0, "新增成功");
    }

    @ApiOperation("删除课件材料")
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public ReturnObject del(@ApiParam("id") @RequestParam Long id) {
        CourseWare courseWare = iCourseWareService.selectById(id);
        if (courseWare == null)
            return new ReturnObject<>(1, "课件id不存在");
        QiniuCloudStorageService qiniu = new QiniuCloudStorageService();
        //删除文件
        qiniu.delete(courseWare.getAttachmentUrl());

        iCourseWareService.deleteById(id);
        return new ReturnObject<>(0, "删除成功");
    }

    @IgnoreAuth
    @ApiOperation("获取课程的课件")
    @RequestMapping(value = "/getCourseWareList", method = RequestMethod.POST)
    public ReturnObject<List<CourseWare>> getCourseWareList(@ApiParam("课程id") @RequestParam Long courseInfoId) {

        return new ReturnObject<>(0, "获取成功", iCourseWareService.selectList(new EntityWrapper<CourseWare>().eq(
                "course_info_id", courseInfoId).orderBy("crt_time", false)));
    }

    @ApiOperation("获取课件材料列表")
    @RequestMapping(value = "/getMyCouresWarePage", method = RequestMethod.POST)
    public ReturnObject<Object> getMyCouresWarePage(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                    @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                    @ApiParam("课程id") Long courseInfoId) {
        EntityWrapper<CourseWare> ew = new EntityWrapper();
        ew.eq("user_id", BaseContextHandler.getUserID()).eq("state", 0);
        if (courseInfoId != null) {
            ew.eq("course_info_id", courseInfoId);
        }
        Page<CourseWare> obj = iCourseWareService.selectPage(new Page<>(offset, limit), ew);

        return new ReturnObject<>(0, "获取成功", obj.getTotal(), obj.getPages(), obj.getRecords());
    }

    @ApiOperation("获取课件材料详情")
    @RequestMapping(value = "/getCourseWareInfo", method = RequestMethod.POST)
    public ReturnObject<CourseWare> getCourseWareInfo(@ApiParam("课件id") Long courseWareId){
        CourseWare courseWare = iCourseWareService.selectById(courseWareId);
        if (courseWare != null)
            courseWare.setUserName(iBaseAppUserService.selectById(courseWare.getUserId()).getUsername());

        return new ReturnObject<>(0, "", courseWare);
    }

    @ApiOperation("根据课程获取课件统计数据")
    @RequestMapping(value = "/getCourseWareCountByCourseInfo", method = RequestMethod.POST)
    public ReturnObject<Object> getCourseWareCountByCourseInfo(@ApiParam("每页条数") @RequestParam(defaultValue = "10") int limit,
                                                               @ApiParam("页数") @RequestParam(defaultValue = "1") int offset,
                                                               @ApiParam("课程id") Long courseInfoId){
        Map<String, Object> map = Maps.newHashMap();
        map.put("list", Lists.newArrayList());
        map.put("totalNum", 0);
        map.put("page", 0);
        map.put("totalPage", 0);
        /*
         * 获取课程材料列表分页list
         */
        if (courseInfoId != null) {
            List<Map<String, Object>> list = iCourseWareService.selectCourseWareCountByCourseId(BaseContextHandler.getUserID(), courseInfoId);
            map.put("list", list);
        } else {
            Page<Map<String, Object>> obj = iCourseWareService.selectCourseWareCount(new Page<>(offset, limit), BaseContextHandler.getUserID());
            map.put("list", obj.getRecords());
            map.put("page", obj.getPages());
            map.put("totalPage", obj.getTotal());
        }


        //获取材料总数
        EntityWrapper<CourseWare> wareEw = new EntityWrapper<>();
        wareEw.eq("user_id", BaseContextHandler.getUserID()).eq("state", 0);
        if (courseInfoId != null)
            wareEw.eq("course_info_id", courseInfoId);
        int totalNum = iCourseWareService.selectCount(wareEw);
        map.put("totalNum", totalNum);

        return new ReturnObject<>(0, "获取成功", map);
    }

    @ApiOperation("删除课程材料")
    @RequestMapping(value = "/delWaraByCourse", method = RequestMethod.POST)
    public ReturnObject delWaraByCourse(@ApiParam("课程") @RequestParam Long courseId) {
        EntityWrapper<CourseWare> ew = new EntityWrapper<>();
        ew.eq("course_info_id", courseId);
        List<CourseWare> list = iCourseWareService.selectList(ew);

        QiniuCloudStorageService qiniu = new QiniuCloudStorageService();
        List<Long> idList = Lists.newArrayList();
        for (CourseWare courseWare : list) {
            //删除文件
            qiniu.delete(courseWare.getAttachmentUrl());
        }
        if (idList.size() > 0)
            iCourseWareService.deleteBatchIds(idList);

        return new ReturnObject<>(0, "删除成功");
    }

}

